package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkMember")
public class CheckMemberController {

    @Reference
    private MemberService memberService;


    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return memberService.findPage(queryPageBean);

    }

    @RequestMapping("/findUserNameList")
    public Result findUserNameList() {
        List<Member> userNameList = null;
        try {
            userNameList = memberService.findUserNameList();
            return new Result(true, "获取数据成功", userNameList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "获取数据失败");
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
            return new Result(true, "添加成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败!");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Member member = memberService.findById(id);
            return new Result(true,"查询会员信息成功!",member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询会员信息失败!");
        }
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.editXXF(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id) {
            String delete = memberService.delete(id);
          if (delete==null){//删除成功
              return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
          }else {//删除失败
              return new Result(false,MessageConstant.DELETE_MEMBER_FAIL,delete);
          }
    }
}
